package com.hs.srm.admin.modules.requisition.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hs.srm.admin.modules.requisition.dao.SamplePurchRequestItemDao;
import com.hs.srm.admin.modules.requisition.entity.SamplePurchRequestItemEntity;
import com.hs.srm.admin.modules.requisition.service.SamplePurchRequestItemService;
import com.hs.srm.admin.modules.requisition.vo.SupplyGoodsEntityVO;
import com.hs.srm.base.common.utils.QueryParam;
import com.hs.srm.common.utils.PageUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ych
 * @since 2020-05-09 09:12:05
 */
@Service("samplePurchRequestItemService")
public class SamplePurchRequestItemServiceImpl extends ServiceImpl<SamplePurchRequestItemDao, SamplePurchRequestItemEntity> implements SamplePurchRequestItemService {

    @Autowired
    private SamplePurchRequestItemDao samplePurchRequestItemDao;

    @Override
    public PageUtils selectDetailPage(Map<String, Object> params) {
        // 从列表页面获取对应的值
        String purchRequestCode = (String) params.get("purchRequestCode");//采购申请单号
        String startDate = (String) params.get("startDate");//需求日期起
        String endDate = (String) params.get("endDate");//需求日期止
        String purchRequestStaff = (String) params.get("purchRequestStaff");//申请人
        String goodsStatus = (String) params.get("goodsStatus");//流程状态
        String purchGroup = (String) params.get("purchGroup");//采购组
        String itemType = (String) params.get("itemType");//样品类型
        Page<SamplePurchRequestItemEntity> page = this.selectPage(
                new QueryParam<SamplePurchRequestItemEntity>(params).getPage(),
                new EntityWrapper<SamplePurchRequestItemEntity>()
                        .like(StringUtils.isNotBlank(purchRequestCode), "purch_request_code", purchRequestCode)
                        .like(StringUtils.isNotBlank(purchRequestStaff), "cost_center", purchRequestStaff)
                        .le(StringUtils.isNotBlank(startDate), "request_date", startDate)
                        .ge(StringUtils.isNotBlank(endDate), "request_date", endDate)
                        .eq(StringUtils.isNotBlank(purchGroup), "purch_group", purchGroup)
                        .eq(StringUtils.isNotBlank(goodsStatus), "goods_status", goodsStatus)
                        .eq(StringUtils.isNotBlank(itemType), "item_type", itemType)
                        .eq("delete_flag", "0")
                        .orderBy("purch_request_code", false));
        page.setRecords(this.samplePurchRequestItemDao.getListSample(page));
        return new PageUtils(page);
    }

    @Override
    public SamplePurchRequestItemEntity findbypkId(String pkId) {
        return this.samplePurchRequestItemDao.selectById(pkId);
    }

    @Override
    public void updateAll(SamplePurchRequestItemEntity samplePurchRequestItemEntity) {
        this.samplePurchRequestItemDao.updateById(samplePurchRequestItemEntity);
    }

    /**
     * 通过请购编码、请购行项目编码将请购单恢复草稿状态
     *
     * @param requestCode
     * @param purchRequestItemCode
     * @return
     */
    @Override
    public int updateStatus(String requestCode, String purchRequestItemCode) {
        return samplePurchRequestItemDao.updateStatus(requestCode, purchRequestItemCode);
    }

    /**
     * 根据样品单id获取分配货源数据
     *
     * @param idList
     * @return
     */
    @Override
    public List<SupplyGoodsEntityVO> getSampleDistributeById(ArrayList<String> idList) {
        return samplePurchRequestItemDao.getSampleDistributeById(idList);
    }

    /**
     * 改变货源状态
     *
     * @param goodsStatus
     * @param npkId
     * @return
     */
    @Override
    public boolean changeGoodsStatus(int goodsStatus, String npkId) {
        SamplePurchRequestItemEntity itemEntity = new SamplePurchRequestItemEntity();
        itemEntity.setGoodsStatus(goodsStatus);
        itemEntity.setPkId(npkId);
        int row = this.baseMapper.updateById(itemEntity);
        return row > 0 ? true : false;
    }

}